﻿/*
/* ---------------------------- */
/* XMLHTTPRequest Enable */
/* ---------------------------- */
function createObject() {
	var request_type;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
		request_type = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else{
		request_type = new XMLHttpRequest();
	}
	return request_type;
}

var http = createObject();





/* -------------------------- */
/* INSERT */
/* -------------------------- */
/* Required: var nocache is a random number to add to request. This value solve an Internet Explorer cache issue */
var nocache = 0;
function insertDomaine() {
	
		// Optional: Show a waiting message in the layer with ID login_response
		document.getElementById('messageBox').innerHTML = "Création en cours ..."
		
		// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
		var domaineLibelle= encodeURI(document.getElementById('newDomaine').value);
		var userId = encodeURI(document.getElementById('userId').value);
		
		// Set te random number to add to URL request
		nocache = Math.random();
		// Pass the login variables like URL variable
		
		http.open('get', 'insertDomaine.php5?domaineLibelle='+domaineLibelle+'&userId=' +userId+'&nocache = '+nocache);
		http.onreadystatechange = insertReply;
		http.send(null);
	}
	
	
function insertPertinence() {
	
		// Optional: Show a waiting message in the layer with ID login_response
		document.getElementById('messageBox').innerHTML = "Création en cours ..."
		
		// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
		var domaineLibelle= encodeURI(document.getElementById('newPertinence').value);
		var userId = encodeURI(document.getElementById('userId').value);
		
		// Set te random number to add to URL request
		nocache = Math.random();
		// Pass the login variables like URL variable
		
		http.open('get', 'insertPertinence.php5?pertinenceLibelle='+domaineLibelle+'&userId=' +userId+'&nocache = '+nocache);
		http.onreadystatechange = insertReply;
		http.send(null);
	}
	
function insertType() {
	
		// Optional: Show a waiting message in the layer with ID login_response
		document.getElementById('messageBox').innerHTML = "Création en cours ..."
		
		// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
		var domaineLibelle= encodeURI(document.getElementById('newType').value);
		var userId = encodeURI(document.getElementById('userId').value);
		
		// Set te random number to add to URL request
		nocache = Math.random();
		// Pass the login variables like URL variable
		
		http.open('get', 'insertType.php5?typeLibelle='+domaineLibelle+'&userId=' +userId+'&nocache = '+nocache);
		http.onreadystatechange = insertReply;
		http.send(null);
	}
function insertTag() {
	
		// Optional: Show a waiting message in the layer with ID login_response
		document.getElementById('messageBox').innerHTML = "Création en cours ..."
		
		// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
		var domaineLibelle= encodeURI(document.getElementById('newTag').value);
		var userId = encodeURI(document.getElementById('userId').value);
		
		// Set te random number to add to URL request
		nocache = Math.random();
		// Pass the login variables like URL variable
		
		http.open('get', 'insertTag.php5?tagLibelle='+domaineLibelle+'&userId=' +userId+'&nocache = '+nocache);
		http.onreadystatechange = insertReply;
		http.send(null);
	}
	
function insertReply() {
	if(http.readyState == 4){
		var response = http.responseText;
		
		document.getElementById('messageBox').innerHTML = response;
		
		// pour le handle des domaines, similaires aux 2 autres de toutes façon
		if (response.indexOf("Nouveau domaine créé !") != -1) {
			var doc;
			doc=document.location.href;
			nouvelUrl = doc.substring(0, doc.indexOf("administration-de-mes-donnees.php5") + 33);
			window.location.replace(nouvelUrl+"#domaineOk");
			location.reload(true);
		}

		// pour le handle des types, similaires aux 2 autres de toutes façon
		if (response.indexOf("Nouveau type créé !") != -1) {
			var doc;
			doc=document.location.href;
			nouvelUrl = doc.substring(0, doc.indexOf("administration-de-mes-donnees.php5") + 33);
			window.location.replace(nouvelUrl+"#typeOk");
			location.reload(true);
		}
		
		// pour le handle des types, similaires aux 2 autres de toutes façon
		if (response.indexOf("Nouveau tag créé !") != -1) {
			var doc;
			doc=document.location.href;
			nouvelUrl = doc.substring(0, doc.indexOf("administration-de-mes-donnees.php5") + 33);
			window.location.replace(nouvelUrl+"#tagOk");
			location.reload(true);
		}
		
		// pour le handle des types, similaires aux 2 autres de toutes façon
		if (response.indexOf("Nouveau nom de pertinence créé !") != -1) {
			var doc;
			doc=document.location.href;
			nouvelUrl = doc.substring(0, doc.indexOf("administration-de-mes-donnees.php5") + 33);
			window.location.replace(nouvelUrl+"#pertOk");
			location.reload(true);
		}
	}
}


// APPPEL AJAX 
function ajaxCall(phpUrl){
	
	
		http.open('get', phpUrl); 
		http.send(null);
		http.onreadystatechange =function(){
		  if(http.readyState == 4){
			document.getElementById("messageBox").innerHTML = http.responseText; 
			ajaxBusy=0;
		  }
		}
	 
 }

// APPPEL AJAX EN LOUPE
/*
function loopThrough(messageDernier, dernier, i,interest_array, phpPage, tags){
  if (interest_array[i]){
    var interest_id = interest_array[i]; 
    
   
		http.open('get', phpPage+interest_id); 
		http.send(null);
		http.onreadystatechange =function(){
		  if(http.readyState == 4){
			ajaxBusy=0;
			document.getElementById("messageBoxForm").innerHTML = http.responseText; 
			i++;
			if (interest_array[i]){
			  loopThrough(messageDernier, dernier, i,interest_array, phpPage, tags);
			}
			else {
				loopThrough2("<b>Fiche crée!</b>", "last", 0, tags, 'newFiche_CreationsTags.php5?userId='+document.getElementById("userId").value+'&tag=');
				/*
				if (dernier=="last") {
					document.getElementById("messageBoxForm").innerHTML = messageDernier; 
				}
			}
		  }
		}
	}
	else {
		loopThrough(messageDernier, dernier, i,interest_array, phpPage, tags);
	}
  }
}

function loopThrough2(messageDernier, dernier, i,interest_array, phpPage){
  if (interest_array[i]){
    var interest_id = interest_array[i]; 
    
    
	if (ajaxBusy==0) {
		
		ajaxBusy=1;
		http.open('get', phpPage+interest_id); 
		http.send(null);
		http.onreadystatechange =function(){
		  if(http.readyState == 4){
			ajaxBusy=0;
			document.getElementById("messageBoxForm").innerHTML = http.responseText; 
			i++;
			if (interest_array[i]){
			  loopThrough(messageDernier, dernier, i,interest_array, phpPage);
			}
			else {
				if (dernier=="last") {
					document.getElementById("messageBoxForm").innerHTML = messageDernier; 
				}
			}
		  }
		}
	}
	else {
		loopThrough(messageDernier, dernier, i,interest_array, phpPage);
	}
  }
}
*/